package com.yanqu.road.server.manager.chatcontrol;

import com.yanqu.road.entity.message.BanChatUserData;
import com.yanqu.road.entity.servercenter.ServerGroup;
import com.yanqu.road.entity.servercenter.ServerInfo;
import com.yanqu.road.entity.union.cross.CrossUnionServerGroup;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.common.CommonProto;
import com.yanqu.road.server.connect.CrossAgentLinkedControl;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manger.ChatControlMgr;
import com.yanqu.road.server.manger.CrossUnionServerGroupMgr;
import com.yanqu.road.server.manger.ServerGroupMgr;
import com.yanqu.road.server.manger.ServerListMgr;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.ConfigHelper;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.utils.config.Config;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class CrossChatControlMgr extends ChatControlMgr {
    @Override
    public boolean reloadData() {
        reloadChatControl(ConfigHelper.getInt("channelId"));
        notifyServerReload();
        return true;
    }

    private static void notifyServerReload() {
        List<BanChatUserData> banChatUserList = getBanChatUserList();
        Set<Long> serverIdSet = new HashSet<>();
        //通知玩家所在的区服，跨服分组区服，商会分组区服重载
        for (BanChatUserData banChatUserData : banChatUserList) {
            if (serverIdSet.contains(banChatUserData.getServerId())) {
                continue;
            }
            serverIdSet.add(banChatUserData.getServerId());
            ServerGroup serverGroup = ServerGroupMgr.getServerGroupByServerId(banChatUserData.getServerId());
            if (null != serverGroup) {
                serverIdSet.addAll(serverGroup.getServerList());
            }
            CrossUnionServerGroup unionServerGroup = CrossUnionServerGroupMgr.getServerGroupByServerId(banChatUserData.getServerId());
            if (null != unionServerGroup) {
                serverIdSet.addAll(unionServerGroup.getServerList());
            }
        }

        for (Long serverId : serverIdSet) {
            if (!CrossAgentLinkedControl.isSeverIdConnection(serverId)) {
                continue;
            }
            CommonProto.NullValueMsg.Builder msg = CommonProto.NullValueMsg.newBuilder();
            YanQuMessage pbMsg = YanQuMessageUtils.buildMessage(Protocol.S_CHAT_CONTROL_DATA_RELOAD_FROM_CROSS, msg);
            MessageHelper.sendPacket(serverId, 0, pbMsg);
        }
    }


}
